* -------------------------- *
* ESTIMATION OF FIRST STAGES * 
* -------------------------- *

*use "${source_data}\Data_Estimation_Elasticities.dta",clear

est clear

global exogenous ln_p_inpf_sbjo 
global endogenous ln_q_sbjo
global instrument ln_q_sbq_inst3b
global interaction relational_quartile
global product type_of_product
global fes gr_sjt

sort *
egen gr_rsjt=group(gr_sjt relational_quartile)
egen gr_Rsjt=group(gr_sjt D_sourcing)

* Sample for estimation of elasticities
capture drop SAMPLE_THETA
gen SAMPLE_THETA=(S_pdsivf==1 & n_ud==1 )


* 1: IV, one theta 

qui: capture ivreghdfe ln_WR_o ${exogenous}  (${endogenous}=${instrument}) if SAMPLE_THETA==1, abs(${fes}) sfirst savefirst savefprefix(ff_)  cluster(gr_sjt)
capture estimates table ff_ln_q_sbjo 
capture mat drop OUT
capture mat define OUT=r(coef)
capture gen gamma_1_iv=OUT[1,1] 		
capture gen gamma_2_iv=OUT[2,1]		   
	  
* 2: Variation of 2 IV with buyer fixed effects

qui: capture ivreghdfe ln_WR_o ${exogenous}  (${endogenous}=${instrument}) if SAMPLE_THETA==1, abs(${fes} b_id) sfirst savefirst savefprefix(ff_) cluster(gr_sjt)
capture estimates table ff_ln_q_sbjo 
capture mat drop OUT
capture mat define OUT=r(coef)
capture gen gamma_1_ivbfe=OUT[1,1]		
capture gen gamma_2_ivbfe=OUT[2,1]		

	  
* 3. IV, one theta, instrumenting for fabric as well

qui: capture ivreghdfe ln_WR_o   ( ${endogenous} ${exogenous}  = ${instrument} ln_cotton_price_x_rate c.ln_cotton_price_x_rate#i1.numf_orig ) ///
 if SAMPLE_THETA==1, abs(${fes})  sfirst savefirst savefprefix(ff_)  cluster(gr_sjt)
capture estimates table ff_ln_q_sbjo 
capture mat drop OUT
capture mat define OUT=r(coef)
capture gen gamma_1_iv2=OUT[1,1]		
capture gen gamma_2_iv2=OUT[2,1]		
capture gen gamma_3_iv2=OUT[3,1]		
capture estimates table ff_ln_p_inpf_sbjo 
capture mat drop OUT
capture mat define OUT=r(coef)
capture gen delta_1_iv2=OUT[1,1] 		
capture gen delta_2_iv2=OUT[2,1]		
capture gen delta_3_iv2=OUT[3,1]				


* 4. IV, thetas by buyer's sourcing strategy

local I 1
foreach i of local I{
capture gen int_exog_`i'=(D_sourcing==`i')*${exogenous}
capture gen int_endog_`i'=(D_sourcing==`i')*${endogenous}
capture gen int_instr_`i'=(D_sourcing==`i')*${instrument}
}


qui: capture  ivreghdfe ln_WR_o ${exogenous} int_exog_1 ///
 (${endogenous} int_endog_1 = ${instrument} int_instr_1 ) if SAMPLE_THETA==1 & D_sourcing!=., abs(gr_Rsjt) cluster(gr_sjt)  sfirst savefirst savefprefix(ff_)
capture estimates table ff_ln_q_sbjo 
capture mat drop OUT
capture mat define OUT=r(coef)
capture gen gamma_q_ivS=OUT[1,1]		
capture gen gamma_q_ivR=OUT[2,1]	   
capture gen delta_q_ivS=OUT[3,1]		
capture gen delta_q_ivR=OUT[4,1]	   
*ivreghdfe ln_WR_o ${exogenous} int_exog_1 ///
 *(${endogenous} int_endog_1 = ${instrument} int_instr_1 ) if SAMPLE_THETA==1 & D_sourcing!=., abs(gr_Rsjt) cluster(gr_sjt)  sfirst savefirst savefprefix(ff_)
capture estimates table ff_int_endog_1  
capture mat drop OUT
capture mat define OUT=r(coef)
capture gen gamma_qR_ivS=OUT[1,1]		
capture gen gamma_qR_ivR=OUT[2,1]	   
capture gen delta_qR_ivS=OUT[3,1]		
capture gen delta_qR_ivR=OUT[4,1]	   

capture drop int_exog* int_endog* int_inst*

gen dirnum = ${dirnum}

keep dirnum gamma* delta*
duplicates drop
order dirnum 

save "${boot}/first_stages_${dirnum}.dta",replace
